﻿using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using SER = CPPEI.ETL.Domain.Services;
using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using VIW = CPPEI.ETL.Domain.Views;

namespace CPPEI.ETL.WebApi
{
    [CPPEI.WebApi.GetAction("ETL", "GetDataSource", "1.0")]
    public class GetDataSourceAction : BaseAction<GetDataSourceParams, GetDataSourceResult>
    {
        protected override ActionResult<GetDataSourceResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            GetDataSourceParams opParams)
        {
            GetDataSourceResult result = new GetDataSourceResult();

            SER.DtSerService ser = new SER.DtSerService();

            ServiceResult<VIW.VDtSourceDetail> serResult = ser.GetDtSource(opParams.DataServiceId, opParams.DataSourceId);

            if (serResult.IsSuccess)
            {
                result.Id = serResult.Data.Id;
                result.Name = serResult.Data.Name;

                if(!string.IsNullOrEmpty(serResult.Data.Value))
                {
                    result.Value = serResult.Data.Value;
                }
                else
                {
                    result.Value = string.Empty;
                }

                if(!string.IsNullOrEmpty(serResult.Data.MappingTable))
                {
                    result.MappingTable = serResult.Data.MappingTable;
                }
                else
                {
                    result.MappingTable = string.Empty;
                }
                if(serResult.Data.MappingTableId!=null)
                {
                    result.MappingTableId = serResult.Data.MappingTableId.Value;
                }
                result.Type = serResult.Data.Type;

                foreach (var eColumn in serResult.Data.Columns)
                {
                    GetDataSourceResult.Column column = new GetDataSourceResult.Column();
                    column.Id = eColumn.Id;
                    column.Name = eColumn.Name;
                    column.Type = eColumn.DataType;
                    if(!string.IsNullOrEmpty(eColumn.Description))
                    {
                        column.Description = eColumn.Description;
                    }
                    else
                    {
                        column.Description = string.Empty;
                    }

                    result.Columns.Add(column);
                }

                foreach (var eParameter in serResult.Data.Parameters)
                {
                    GetDataSourceResult.Parameter parameter = new GetDataSourceResult.Parameter();
                    parameter.Id = eParameter.Id;
                    parameter.Name = eParameter.Name;
                    parameter.Type = eParameter.DataType;

                    if (!string.IsNullOrEmpty(eParameter.Description))
                    {
                        parameter.Description = eParameter.Description;
                    }
                    else
                    {
                        parameter.Description = string.Empty;
                    }

                    parameter.DefaultValue = DefaultValue.GetValue(eParameter.DefaultValue, string.Empty);

                    result.Parameters.Add(parameter);
                }

                return new ActionResult<GetDataSourceResult>(result);
            }
            return new ActionResult<GetDataSourceResult>(false, serResult.ErrorCode, serResult.Message, null);
        }
    }

    public class GetDataSourceParams
    {
        [Parameter]
        public Guid DataServiceId { get; set; }
        [Parameter]
        public Guid DataSourceId { get; set; }
    }

    public class GetDataSourceResult
    {
        public Guid Id { get; set; }

        public string Name { get; set; }

        public string MappingTable { get; set; }
        public Guid MappingTableId { get; set; }

        public int Type { get; set; }

        public string Value { get; set; }

        public List<Parameter> Parameters { get; set; }
        public List<Column> Columns { get; set; }

        public GetDataSourceResult()
        {
            Parameters = new List<Parameter>();
            Columns = new List<Column>();
        }

        public class Parameter
        {
            public Guid Id { get; set; }

            public string Name { get; set; }

            public string Type { get; set; }

            public string Description { get; set; }

            public string DefaultValue { get; set; }
        }

        public class Column
        {
            public Guid Id { get; set; }

            public string Name { get; set; }

            public string Type { get; set; }

            public string Description { get; set; }
        }
    }
}
